package com.ticket.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ticket.common.exception.base.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ticket.system.mapper.YanchuRechargeRulesMapper;
import com.ticket.system.domain.YanchuRechargeRules;
import com.ticket.system.service.IYanchuRechargeRulesService;
import org.springframework.util.CollectionUtils;

/**
 * 充值活动规则Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-07
 */
@Service
public class YanchuRechargeRulesServiceImpl implements IYanchuRechargeRulesService 
{
    @Autowired
    private YanchuRechargeRulesMapper yanchuRechargeRulesMapper;

    /**
     * 查询充值活动规则
     * 
     * @param id 充值活动规则主键
     * @return 充值活动规则
     */
    @Override
    public YanchuRechargeRules selectYanchuRechargeRulesById(Long id)
    {
        return yanchuRechargeRulesMapper.selectYanchuRechargeRulesById(id);
    }

    /**
     * 查询充值活动规则列表
     * 
     * @param yanchuRechargeRules 充值活动规则
     * @return 充值活动规则
     */
    @Override
    public List<YanchuRechargeRules> selectYanchuRechargeRulesList(YanchuRechargeRules yanchuRechargeRules)
    {
        return yanchuRechargeRulesMapper.selectYanchuRechargeRulesList(yanchuRechargeRules);
    }

    /**
     * 新增充值活动规则
     * 
     * @param yanchuRechargeRules 充值活动规则
     * @return 结果
     */
    @Override
    public int insertYanchuRechargeRules(YanchuRechargeRules yanchuRechargeRules)
    {
        yanchuRechargeRules.setCreatTime(new Date());
        //校验充值金额和排序字段值是否存在
        if (checkAmountAndOrderNum(yanchuRechargeRules.getId(),yanchuRechargeRules.getRecAmount(),null)){
            throw new BaseException("充值金额已存在");
        }
        if (checkAmountAndOrderNum(yanchuRechargeRules.getId(),null,yanchuRechargeRules.getOrderNum())){
            throw new BaseException("排序值已存在");
        }
        return yanchuRechargeRulesMapper.insertYanchuRechargeRules(yanchuRechargeRules);
    }

    /**
     * 修改充值活动规则
     * 
     * @param yanchuRechargeRules 充值活动规则
     * @return 结果
     */
    @Override
    public int updateYanchuRechargeRules(YanchuRechargeRules yanchuRechargeRules)
    {
        yanchuRechargeRules.setCreatTime(new Date());
        if (checkAmountAndOrderNum(yanchuRechargeRules.getId(),yanchuRechargeRules.getRecAmount(),null)){
            throw new BaseException("充值金额已存在");
        }
        if (checkAmountAndOrderNum(yanchuRechargeRules.getId(),null,yanchuRechargeRules.getOrderNum())){
            throw new BaseException("排序值已存在");
        }
        return yanchuRechargeRulesMapper.updateYanchuRechargeRules(yanchuRechargeRules);
    }

    /**
     * 批量删除充值活动规则
     * 
     * @param ids 需要删除的充值活动规则主键
     * @return 结果
     */
    @Override
    public int deleteYanchuRechargeRulesByIds(Long[] ids)
    {
        return yanchuRechargeRulesMapper.deleteYanchuRechargeRulesByIds(ids);
    }

    /**
     * 删除充值活动规则信息
     * 
     * @param id 充值活动规则主键
     * @return 结果
     */
    @Override
    public int deleteYanchuRechargeRulesById(Long id)
    {
        return yanchuRechargeRulesMapper.deleteYanchuRechargeRulesById(id);
    }

    /**
     * 校验是否存在相同充值金额
     *
     * @param id
     * @param recAmount 与orderNum二者只存在一个
     * @param orderNum  与recAmount二者只存在一个
     * @return
     */
    private Boolean checkAmountAndOrderNum(Long id, BigDecimal recAmount,Integer orderNum){
        YanchuRechargeRules rules = new YanchuRechargeRules();
        rules.setRecAmount(recAmount);
        rules.setOrderNum(orderNum);
        List<YanchuRechargeRules> yanchuRechargeRules = yanchuRechargeRulesMapper.selectYanchuRechargeRulesList(rules);
        if (!CollectionUtils.isEmpty(yanchuRechargeRules)){
            if (null == id){
                //新增
                return true;
            }else {
                if (!id.equals(yanchuRechargeRules.get(0).getId())){
                    return true;
                }
            }
        }
        return false;
    }
}
